package com.cyz.admin.controller;

import com.cyz.common.dto.MenuDto;
import com.cyz.admin.service.Admin_MenuService;
import com.cyz.common.utils.R;
import com.cyz.common.validated.AddGroup;
import com.cyz.common.validated.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/menus")
@Api(tags = "管理端菜单相关接口")
public class MenusController {

    @Autowired
    private Admin_MenuService adminAdminMenuService;

    @PreAuthorize("@ps.hasPermission('system:menu:list')")
    @ApiOperation(value = "查询菜单")
    @GetMapping("/queryMenus")
    public R queryMenus(){
        return adminAdminMenuService.queryMenus();
    }

    @PreAuthorize("@ps.hasPermission('system:menu:list')")
    @PostMapping("/addMenu")
    @ApiOperation(value = "增加菜单")
    public R addMenu(@Validated(AddGroup.class) @RequestBody MenuDto menuDto){
        return adminAdminMenuService.addMenu(menuDto);
    }

    @PreAuthorize("@ps.hasPermission('system:menu:list')")
    @PutMapping("/updateMenu")
    @ApiOperation(value = "更新菜单")
    public R updateMenu(@Validated(UpdateGroup.class) @RequestBody MenuDto menuDto){
        return adminAdminMenuService.updateMenu(menuDto);
    }

    @PreAuthorize("@ps.hasPermission('system:menu:list')")
    @DeleteMapping("/deleteMenu")
    @ApiOperation(value = "删除菜单")
    public R deleteMenus(Integer id){
        return adminAdminMenuService.deleteMenu(id);
    }

    @PreAuthorize("@ps.hasPermission('system:menu:list')")
    @ApiOperation(value = "查询菜单树")
    @GetMapping("/queryMenuTrees")
    public R queryMenuTrees(){
        return adminAdminMenuService.queryMenuTrees();
    }
}
